﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace HZP
{
    public partial class Form1 : Form
    {
        private DataTable MainDT;
        public Form1()
        {
            InitializeComponent();

            //交易记录
            UTIL.InitDGVColumns(dgvList, new ColConfig[]
            {
                new ColConfig {ColName="编号",FieldName="ID",Freezeable=true ,Width=60},
                new ColConfig {ColName="化妆品名称",FieldName="HZPMC",Editable=true  ,ColType="combo",Width=150},
                new ColConfig {ColName="姓名",FieldName="JYR",Editable=true ,ColType="combo" },
                new ColConfig {ColName="日期",FieldName="RQ",Editable=false,Width=100 },
                new ColConfig {ColName="数量",FieldName="SL",Editable=true },
                new ColConfig {ColName="总价",FieldName="PRICE",Editable=true },
                new ColConfig {ColName="备注",FieldName="BZ",Editable=true,Width=200 }
            });
            dgvList.UserAddedRow += (object sender, DataGridViewRowEventArgs e) => dgvList.Rows[e.Row.Index - 1].Cells["RQ"].Value = DateTime.Now;
            dgvList.DataError += (object sender, DataGridViewDataErrorEventArgs e) => { };
            //日期
            dtimeStart.Value = DateTime.Today;
            dtimeEnd.Value = DateTime.Today.AddDays(1);

            showList();

            //汇总信息
            UTIL.InitDGVColumns(dgvTJ, new ColConfig[]
            {
                new ColConfig {ColName="名称",FieldName="MC",Freezeable=true ,Width=180},
                new ColConfig {ColName="总数",FieldName="ZS"},
                new ColConfig {ColName="总价",FieldName="ZJ"  }
            });
            dgvTJ.Columns["MC"].DefaultCellStyle.NullValue = "合计";
            showTJ();
        }
        //绑定数据
        private void showList()
        {
            //绑定化妆品列表
            var col = dgvList.Columns["HZPMC"] as DataGridViewComboBoxColumn;
            col.DataSource = DB.HZP.Query("select mc+'| '+ckjg as INFO,MC　from hzpinfo order by px");
            col.DisplayMember = "INFO";
            col.ValueMember = "MC";
            //绑定顾客列表
            col = dgvList.Columns["JYR"] as DataGridViewComboBoxColumn;
            col.DataSource = DB.HZP.Query("select *　from people");
            col.DisplayMember = "NAME";
            col.ValueMember = "NAME";
            //绑定数据
            string start = dtimeStart.Value.ToString("yyyy-MM-dd");
            string end = dtimeEnd.Value.ToString("yyyy-MM-dd");
            MainDT = DB.HZP.Query("select * from hzp where rq>='"+start+"' and rq<='"+end+"' order by rq" );
            bsList.DataSource = MainDT;
        }
        //绑定分析数据
        private void showTJ()
        {
            string start = dtimeStart.Value.ToString("yyyy-MM-dd");
            string end = dtimeEnd.Value.ToString("yyyy-MM-dd");
            if (cboxTjfs.Text=="按物品")
            {
                dgvTJ.DataSource = DB.HZP.Query("select  hzpmc as MC, sum(sl) ZS,sum(price) ZJ from hzp where rq>='" + start + "' and rq<='" + end + "' and sl>0 group by hzpmc with cube order by ZJ DESC");
            }
            else
            {
                dgvTJ.DataSource = DB.HZP.Query("select  JYR AS MC, sum(sl) ZS,sum(price) ZJ from hzp where rq>='" + start + "' and rq<='" + end + "' and sl>0 group by JYR with cube order by ZJ DESC");
            }
            
        }
        private void btnQuery_Click(object sender, EventArgs e)
        {
            showList();
            showTJ();
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确定要删除这条记录吗？ 删除后便无法恢复！") == DialogResult.OK)
            {
                foreach (DataGridViewRow select in dgvList.SelectedRows)
                {
                    dgvList.Rows.Remove(select);
                }
                btnSave.PerformClick();
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            DB.HZP.Update(bsList.DataSource as DataTable, "select * from hzp where 1=2");
            btnQuery.PerformClick();
        }

        private void 化妆品信息ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            HZPINFO frm = new HZPINFO();
            frm.ShowDialog();
            showList();
        }

        private void 客户档案ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            USERINFO frm = new USERINFO();
            frm.ShowDialog();
            showList();
        }

        private void btnToday_Click(object sender, EventArgs e)
        {
            dtimeStart.Value = DateTime.Today;
            dtimeEnd.Value = DateTime.Today.AddDays(1);
        }

        private void btnMonth_Click(object sender, EventArgs e)
        {
            dtimeStart.Value = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1);
            dtimeEnd.Value = DateTime.Today.AddDays(1);
        }

        private void btnYear_Click(object sender, EventArgs e)
        {
            dtimeStart.Value  = new DateTime(DateTime.Today.Year, 1, 1);
            dtimeEnd.Value = DateTime.Today.AddDays(1);
        }

        private void btnKC_Click(object sender, EventArgs e)
        {
            dgvTJ.DataSource = DB.HZP.Query("select  hzpmc as MC, sum(sl) ZS,sum(price) ZJ from hzp  group by hzpmc with cube order by ZJ DESC");
        }

        private void cboxTjfs_SelectedValueChanged(object sender, EventArgs e)
        {
            showTJ();
        }


    }
}
